package com.szt.sy.controller.system.rolemenu;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.plugins.Page;
import com.szt.sy.anontation.Authority;
import com.szt.sy.anontation.AuthorityConfig;
import com.szt.sy.anontation.SysLog;
import com.szt.sy.common.AuthorityCommon;
import com.szt.sy.common.RegexConst;
import com.szt.sy.controller.base.BaseController;
import com.szt.sy.entity.system.RoleMenu;
import com.szt.sy.service.system.rolemenu.RoleMenuService;
import com.szt.sy.util.date.DateUtil;
import com.szt.sy.util.excel.ObjectExcelView;
import com.szt.sy.vo.rep.base.RepBaseVO;
import com.szt.sy.vo.req.page.ReqPageVO;

/**
 * @author wwy
 *
 *         <p>
 *         说明：角色-菜单权限控制器<br/>
 *         创建时间：2018-03-14
 *         </p>
 */
@Controller
@AuthorityConfig(prefix = "/admin/rolemenu/list")
@RequestMapping(value = "admin/rolemenu")
public class RoleMenuController extends BaseController {
	
	@Autowired
	private RoleMenuService rolemenuService;
	
	/**
	 * 去列表页面
	 * 
	 */
	@Authority(name = AuthorityCommon.LIST)
	@GetMapping(value = "/list")
	public ModelAndView goPage(@RequestParam Map<String, Object> map) {
		ModelAndView mv = this.getModelAndView();
		mv.setViewName("system/rolemenu/rolemenu_list");
		mv.addObject("queryParam", map);
		// 按钮权限
		
		return mv;
	}
	
	/**
	 * 列表数据
	 */
	@SysLog("查询角色-菜单权限列表数据")
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Authority(name = AuthorityCommon.LIST)
	@PostMapping(value = "/list")
	@ResponseBody
	public RepBaseVO list(ReqPageVO pageVO, @RequestParam Map<String, Object> map) {
		RepBaseVO repVO = new RepBaseVO<>();
		processParams(map);
		Page<RoleMenu> page = processPage(pageVO);
		page = rolemenuService.list(page, map);
		List<RoleMenu> varList = page.getRecords();
		Map<String, Object> resultInfo = new HashMap<>(2);
		resultInfo.put("data", varList);
		resultInfo.put("total", page.getTotal());
		return repVO.setCommonSuccess().setData(resultInfo);
	}
	
	/**
	 * 去新增页面
	 */
	@Authority(name = AuthorityCommon.ADD)
	@GetMapping(value = "/add")
	public ModelAndView goAdd(@RequestParam Map<String, Object> map) {
		ModelAndView mv = this.getModelAndView();
		mv.setViewName("system/rolemenu/rolemenu_edit");
		RoleMenu entity = new RoleMenu();
		mv.addObject("msg", "add");
		mv.addObject("entity", entity);
		mv.addObject("pd", map);
		return mv;
	}
	
	/**
	 * 保存
	 */
	@SysLog("添加角色-菜单权限数据")
	@Authority(name = AuthorityCommon.ADD)
	@PostMapping(value = "/add")
	@ResponseBody
	public RepBaseVO save(RoleMenu entity) {
		RepBaseVO repVO = new RepBaseVO();
		entity.setVersion(1L);
		entity.setAddDate(new Date());
		rolemenuService.insert(entity);
		return repVO.setCommonSuccess();
	}
	
	/**
	 * 去修改页面
	 */
	@Authority(name = AuthorityCommon.EDIT)
	@GetMapping(value = "/edit")
	public ModelAndView goEdit(Long id) {
		ModelAndView mv = this.getModelAndView();
		RoleMenu entity = rolemenuService.selectById(id);
		mv.setViewName("system/rolemenu/rolemenu_edit");
		mv.addObject("msg", "edit");
		mv.addObject("entity", entity);
		return mv;
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改角色-菜单权限数据")
	@Authority(name = AuthorityCommon.EDIT)
	@PostMapping(value = "/edit")
	@ResponseBody
	public RepBaseVO edit(RoleMenu entity) {
		RepBaseVO repVO = new RepBaseVO<>();
		rolemenuService.updateById(entity);
		return repVO.setCommonSuccess();
	}
	
	/**
	 * 删除
	 */
	@SysLog("删除角色-菜单权限数据")
	@Authority(name = AuthorityCommon.DELETE)
	@PostMapping(value = "/delete")
	@ResponseBody
	public RepBaseVO delete(Long id) {
		rolemenuService.deleteById(id);
		return RepBaseVO.newIntance().setCommonSuccess();
	}
	
	/**
	 * 批量删除
	 */
	@SysLog("批量删除角色-菜单权限数据")
	@SuppressWarnings({ "rawtypes" })
	@Authority(name = AuthorityCommon.DELETE)
	@PostMapping(value = "/deleteAll")
	@ResponseBody
	public RepBaseVO deleteAll(@RequestParam("ids[]") List<String> ids) {
		RepBaseVO repVO = new RepBaseVO<>();
		if (null != ids && !ids.isEmpty()) {
			rolemenuService.deleteBatchIds(ids);
		}
		return repVO.setCommonSuccess();
	}
	
	/**
	 * 导出到excel
	 */
	@SysLog("导出角色-菜单权限excel")
	@Authority(name = AuthorityCommon.LIST)
	@PostMapping(value = "/excel")
	public ModelAndView exportExcel(@RequestParam Map<String, Object> map) {
		ModelAndView mv = new ModelAndView();
		processParams(map);
		Map<String, Object> dataMap = new HashMap<String, Object>(2);
		List<String> titles = new ArrayList<String>();
		titles.add("角色");
		titles.add("菜单id");
		dataMap.put("titles", titles);
		List<RoleMenu> varOList = rolemenuService.listAll(map);
		List<Map<String, Object>> varList = new ArrayList<Map<String, Object>>();
		for (RoleMenu entity : varOList) {
			Map<String, Object> vpd = new HashMap<String, Object>(30);
			vpd.put("var1", entity.getRoleId());
			vpd.put("var2", entity.getMenuId());
			varList.add(vpd);
		}
		
		dataMap.put("varList", varList);
		ObjectExcelView erv = new ObjectExcelView();
		mv = new ModelAndView(erv, dataMap);
		return mv;
	}
	
	/***
	 * 处理输入参数
	 * 
	 * @param map
	 */
	private void processParams(Map<String, Object> map) {
		String sStartDate = (String) map.get("startDate");
		String sEndDate = (String) map.get("endDate");
		if (StringUtils.isBlank(sStartDate)) {
			map.put("startDate", null);
		} else {
			map.put("startDate", DateUtil.getStartTimeOfDate(sStartDate));
		}
		if (StringUtils.isBlank(sEndDate)) {
			map.put("endDate", null);
		} else {
			map.put("endDate", DateUtil.getEndTimeOfDate(sEndDate));
		}
		String sortDirection = (String) map.get("sortDirection");
		String sortColumn = (String) map.get("sortColumn");
		if (StringUtils.isNotBlank(sortColumn) && !sortColumn.matches(RegexConst.WORD)) {
			map.remove("sortColumn");
		}
		if (StringUtils.isNotBlank(sortDirection) && !sortDirection.matches(RegexConst.WORD)) {
			map.remove("sortDirection");
		}
	}
	
	/***
	 * 处理分页参数
	 * 
	 * @param pageVO
	 * @return
	 */
	private Page<RoleMenu> processPage(ReqPageVO pageVO) {
		Page<RoleMenu> page = new Page<>(pageVO.getCurrent(), pageVO.getSize());
		return page;
	}
	
}
